package com.gzhryc.socket;

import java.util.Date;

public abstract class SocketNode {
	
	protected SocketConfig config;

	protected final Date createDate; // 创建时间
	protected Date connectedDate; 	 // 链接时间
	protected Date lastSendDate; 	 // 最后发送时间
	protected Date lastReceiveDate;  // 最后接受时间

	public SocketNode() {
		this.createDate = new Date();
	}

	public void runTask(Runnable runnable) {
	}

	public SocketConfig getConfig() {
		return this.config;
	}

	public abstract void start();

	public abstract void startService();

	public abstract boolean isConnected();
	
	public abstract void reconnect();

	public abstract String getId();

	/**
	 * 发送消息
	 * 
	 * @param message
	 * @return
	 */
	public abstract boolean writeAndFlush(SocketMessage message);

	/**
	 * 关闭连接
	 */
	public abstract void close();

	public <T> T changeTo(Class<T> pClass) {
		if (pClass.isInstance(this)) {
			return pClass.cast(this);
		}
		return null;
	}

	public Date getConnectedDate() {
		return connectedDate;
	}
	
	public void setConnectedDate(Date connectedDate) {
		this.connectedDate = connectedDate;
	}
	
	public Date getLastSendDate() {
		return lastSendDate;
	}
	
	public void setLastSendDate(Date lastSendDate) {
		this.lastSendDate = lastSendDate;
	}
	
	public Date getLastReceiveDate() {
		return lastReceiveDate;
	}
	
	public void setLastReceiveDate(Date lastReceiveDate) {
		this.lastReceiveDate = lastReceiveDate;
	}
	
	public Date getCreateDate() {
		return createDate;
	}
	
	/**
	 * 订阅主题（仅MQTT协议使用）
	 * @param topic
	 * @param qos
	 * @return
	 */
	public boolean subscribe(String topic, int qos){return false;}
	
	/**
	 * 取消订阅的主题（仅MQTT协议使用）
	 * @param topic
	 * @return
	 */
	public boolean unsubscribe(String topic){return false;}
}
